package ru.study.thinkin.chapter11;

import java.util.*;

class ReversibleArrayList<T> extends ArrayList<T>  {
	public ReversibleArrayList(Collection<T> c) {
		super(c);
	}	
	
	public Iterable<T> getRevIterator() {
	
		return new Iterable<T>() {
			@Override
			public Iterator<T> iterator() {				
				return new IteratorImpl();
			}
			
		};
	}	
	
	class IteratorImpl implements Iterator {
				
				int index = 1;

				@Override
				public boolean hasNext() {
					return index < size();
				}

				@Override
				public T next() {
					return get(size() - index++);
				}

				@Override
				public void remove() {
					throw new UnsupportedOperationException();
				}		
	}
}

public class AdapterMethodIdiom {
	
	public static void main(String[] args) {
		ReversibleArrayList<String> ral = new ReversibleArrayList<String>(
			Arrays.asList("to be or nor to be".split(" ")));	
		
	  for(String s : ral) {
		  System.out.println(s); 
	  }
	  System.out.println("=-=-");
	  for(String s : ral.getRevIterator()) {
		  System.out.println(s); 
	  }	  
	  
	}

}
